<template>
  <div class="table-page-search-wrapper">
    <el-form :inline="true" :model="form" ref="form" label-width="70px">
      <el-row :gutter="24">
        <el-col :span="6">
          <el-form-item label="供应商">
            <supplier-select v-model="form.supplierCode" @select="handleSelect" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="不良类型">
            <el-select v-model="form.badType" placeholder="请选择" clearable>
              <el-option
                :value="item.value"
                v-for="(item,index) in badTypeArr"
                :key="index"
                :label="item.title"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="时间段">
            <el-date-picker
              value-format="yyyy-MM-dd"
              style="width: 100%"
              type="daterange"
              v-model="form.date"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item>
            <el-tooltip class="item" effect="dark" content="查询" placement="top">
              <el-button type="primary" class="mr20" @click="handleSearch" icon="el-icon-search" />
            </el-tooltip>
            <el-tooltip class="item" effect="dark" content="重置" placement="top">
              <el-button @click="handleReset" icon="el-icon-refresh-right" />
            </el-tooltip>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
import SupplierSelect from '../components/SupplierSelect'
import { getDicData } from '@/utils/util'

export default {
  components: {
    SupplierSelect
  },
  data() {
    return {
      form: {
        supplierCode: null,
        badType: null,
        date: null,
        pageNo: 1
      },
      badTypeArr: []
    }
  },
  mounted() {
    this.badTypeArr = getDicData('002', '002006')
  },
  methods: {
    handleSearch() {
      this.$refs.form.validate(valid => {
        if (valid) {
          const { supplierCode, badType, date } = this.form
          let startTime = null
          let endTime = null
          if (date !== null) {
            startTime = date[0]
            endTime = date[1]
          }
          const param = { supplierCode, badType, startTime, endTime }
          this.$emit('search', param)
        }
      })
    },
    handleReset() {
      this.$nextTick(() => {
        this.$refs.form.resetFields()
      })
      this.$emit('search', {})
    },
    handleSelect(value) {
      this.$set(this.form, 'supplierCode', value)
    }
  }
}
</script>
